package cn.duhengkui.national.valuelist.core.data.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**   
 * <table border="1">
 *	<tr><td>包名称：</td><td>cn.duhengkui.national.valuelist.core.data.util</td></tr>
 *	<tr><td>类名称：</td><td>ReflectUtil</td></tr>
 *	<tr><td>类描述：</td><td>自定义反射工具</td></tr>
 *	<tr><td>创建人：</td><td>杜恒奎</td></tr>
 *  <tr><td>创建时间：</td><td>2018年6月7日/下午3:07:27</td></tr> 
 *  <tr><td>版本：</td><td>1.0.0</td></tr>
 * </table>   
 *     
 */
public class ReflectUtil {
	
	
	/**
	* @Title: captureName
	* @Description: 首字母大写
	* @param name
	* @return
	* @return String
	*/ 
	public static  String captureName(String name) {
		name = name.substring(0, 1).toUpperCase() + name.substring(1);
		return name;
	}
	
	/**
	* @Title: getSubValue
	* @Description: 获对象下属性值
	* @param obj
	* @param fieldName
	* @return
	* @throws NoSuchMethodException
	* @throws SecurityException
	* @throws IllegalAccessException
	* @throws IllegalArgumentException
	* @throws InvocationTargetException
	* @return Object
	*/ 
	private static Object getSubValue(Object obj, String fieldName) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		
			Method method = obj.getClass().getDeclaredMethod("get" +captureName(fieldName));
			Object result = method.invoke(obj);
			return result;
		
	}
	
	/**
	* @Title: getValue
	* @Description: 获取Map对象中相应属性值（属性可以用‘.’获取子属性）
	* @param objMap
	* @param key
	* @return
	* @throws NoSuchMethodException
	* @throws SecurityException
	* @throws IllegalAccessException
	* @throws IllegalArgumentException
	* @throws InvocationTargetException
	* @return Object
	*/ 
	public static Object getValue(Map<String,Object> objMap, String key) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		String[] keyTree = key.split("\\.");
		Object orlObj = objMap.get(keyTree[0]);
		if(keyTree.length > 1){
			for(int i = 1; i < keyTree.length;i++){
				orlObj = getSubValue(orlObj,keyTree[i]);
			}
		}
		return orlObj;
		
	}

}
